package com.wmlive.hhvideo.heihei.splash;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.umeng.analytics.MobclickAgent;
import com.wmlive.hhvideo.application.AppStatusManager;
import com.wmlive.hhvideo.application.DCApplication;
import com.wmlive.hhvideo.common.GlobalParams;
import com.wmlive.hhvideo.common.base.DcBaseActivity;
import com.wmlive.hhvideo.heihei.beans.splash.InitCatchData;
import com.wmlive.hhvideo.heihei.beans.splash.InitResponse;
import com.wmlive.hhvideo.heihei.mainhome.activity.MainActivity;
import com.wmlive.hhvideo.heihei.mainhome.activity.MainNewActivity;
import com.wmlive.hhvideo.heihei.splash.presenter.SplashPresenter;
import com.wmlive.hhvideo.heihei.splash.view.SplashView;
import com.wmlive.hhvideo.service.DCService;
import com.wmlive.hhvideo.utils.KLog;

import cn.wmlive.hhvideo.BuildConfig;
import cn.wmlive.hhvideo.R;

public class SplashActivity extends DcBaseActivity<SplashPresenter> implements SplashView {

    private boolean isLaunchError;//启动失败

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_splash;
    }

    @Override
    protected SplashPresenter getPresenter() {
        return new SplashPresenter(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        AppStatusManager.getInstance().setAppStatus(AppStatusManager.STATUS_NORMAL);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initData() {
        super.initData();
        startDCService();
        initMobclickAgentParams();
        InitResponse initResponse = InitCatchData.getInitCatchData();
        if (initResponse != null && !TextUtils.isEmpty(initResponse.getVersion())) {
            getWeakHandler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    KLog.i("====直接进入Main");
                    try {
                        //从推送过来的消息
                        MainNewActivity.startActivity(SplashActivity.this, getIntent().getStringExtra(MainNewActivity.KEY_CONTENT));
                    } catch (Exception e) {
                        MainNewActivity.startActivity(SplashActivity.this);
                    }
                    SplashActivity.this.finish();
                }
            }, 1000);
        } else {
            dataInit();
        }
    }

    private void startDCService() {
        Intent intent = new Intent(this, DCService.class);
        startService(intent);
    }


    @Override
    protected void onSingleClick(View v) {

    }

    /**
     * 调用init接口
     */
    private void dataInit() {
        isLaunchError = false;
        KLog.i("====从网络获取Init数据");
        presenter.init();
    }

    @Override
    public void initApiError(String msg) {
        isLaunchError = true;
        showToast(msg);
        showStatusPage(2, msg + "\n请稍后尝试重启App\n或者检查手机网络设置");
//        Navigator.getInstance().navigateToMain(SplashActivity.this);
        MainNewActivity.startActivity(this);
        finish();
    }

    @Override
    public void initApiSucceed() {
        KLog.i("initApiSucceed 加载成功");
        GlobalParams.StaticVariable.sInitFromLocal = false;
//        Navigator.getInstance().navigateToMain(SplashActivity.this);
        MainNewActivity.startActivity(this);
        finish();
    }

    @Override
    public void onInitComplete() {
        KLog.i("onInitComplete");
        finish();
    }

    @Override
    public void handleNetError() {
        showStatusPage(3, "加载失败");
    }

    @Override
    public void onOptionClick(View view) {
        super.onOptionClick(view);
//        dataInit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (isLaunchError) {
            finish();
            DCApplication.getDCApp().exitApp();
        } else {
            //启动正常，屏蔽back事件
        }
        return true;
    }

    /**
     * 初始化友盟统计功能
     */
    private void initMobclickAgentParams() {
        MobclickAgent.setDebugMode(BuildConfig.DEBUG_SWITCH);//是否是集成测试环境
        MobclickAgent.openActivityDurationTrack(false);//默认页面的统计
        MobclickAgent.setScenarioType(getApplicationContext(), MobclickAgent.EScenarioType.E_UM_NORMAL);
        MobclickAgent.setCatchUncaughtExceptions(false);//禁用crash 捕获
    }
}
